import {useUrlBreaker} from "@/compositions/main/useUrlBreaker";
import {addBtnPermission} from "@/utils";
import {ref} from "vue";

export function usePermission() {
  const store = useStore()
  const {serviceType} = useUrlBreaker()
  const merchantShow = ref(false) // TODO 未见页面有引用这个变量 待删除

  const condition: (id:string) => boolean = (id) => {
    return !!store.state.user.allPermission.find((j:any) => j.serviceType == id)
  };

  const condition2: () => boolean = () => {
    let flagtemp = true;
    if (
      store.state.user.currentService != "Merchant" &&
      store.state.user.allPermission.length == 1
    ) {
      flagtemp = false;
    }
    return flagtemp;
  }

  onBeforeMount(() => {
    const temp = store.state.user.allPermission.filter((i: any) => {
      return i.serviceType == serviceType;
    })[0];
    if (temp) {
      addBtnPermission(temp.permissionLayer, temp.serviceType + "Btn");
    }

    store.state.user.allPermission.forEach((j: any) => {
      if (j.serviceType == "MERCHANT") {
        merchantShow.value = true;
      }
    });
  })

  return {
    merchantShow,

    condition,
    condition2
  }
}
